unit u_partic;

interface

uses
 sysutils,
 system.Drawing,
// u_form,
 Microsoft.DirectX,
Microsoft.DirectX.Direct3D,
Microsoft.DirectX.Direct3DX;

type

TColor = record
  R, G, B : Double;
end;

particule = record
    X, Y,z, to_x, to_y : single;
    delta_x, delta_y,delta_z : single;
    l:boolean;
end;

TParticle = class
    private
      particle_mesh : Mesh;
      particle_texture,part : Texture;
      t_tab_part : Array of particule;
    public
      constructor Create(dev : device);
      procedure Render(dev:device);
      destructor Destroy; override;
      procedure add_part(nbr, x, y, vx, vy : integer);
end;


implementation

constructor TParticle.Create(dev : device);
var
  i:integer;
  begin
    inherited create;
    SetLength(t_tab_part, 0);
    particle_mesh := mesh.FromFile('meshes\matias.x', meshflags.SystemMemory, dev);
    particle_texture := Textureloader.FromFile(dev, 'meshes\particle.bmp');
    part := Textureloader.FromFile(dev, 'meshes\pipi.bmp');
  end;

procedure tparticle.add_part(nbr, x, y, vx, vy : integer);
var
  i, j : integer;
  len : integer;
  v_tmp : single;
  begin
    len := nbr;
    SetLength(t_tab_part, len );
    for i := 0 to len - 1 do
      begin
        v_tmp := (Random(50) + 25);
        t_tab_part[i].X := x;
        t_tab_part[i].Y := y;
        t_tab_part[i].z := 0.6;
        if (vx-x)>0 then
          t_tab_part[i].to_x := vx+0.5
        else
          t_tab_part[i].to_x := vx-0.5;
        if (vy-y)>0 then
          t_tab_part[i].to_y := vy+0.5
        else
          t_tab_part[i].to_y := vy-0.5;
        t_tab_part[i].l:= true;
          t_tab_part[i].delta_x := (vx - x)/v_tmp ;


          t_tab_part[i].delta_y := (vy - y)/v_tmp;
          t_tab_part[i].delta_z := 0;

      end;
  end;

procedure TParticle.Render(dev:device);
var
  i, j : integer;
  k:single;
  l:boolean;
  len : integer;
  begin
  len := Length(t_tab_part);
  i := 0;
//  l:=true;
//  k := (Random(500) + 25)/2;
  while i < len do
    begin
//      if (not (t_tab_part[i].l)) then
//         begin
////           if ((t_tab_part[i].z < 0.01) or (t_tab_part[i].delta_z < 0)) then
////            begin
////              t_tab_part[i].delta_z := - t_tab_part[i].delta_z;
////              t_tab_part[i].delta_z := t_tab_part[i].delta_z * 0.9;
////            end;
//
//              // Ajout de la gravite
//            t_tab_part[i].delta_z := t_tab_part[i].delta_z - 0.0002;
//         end;
//          t_tab_part[i].delta_z := t_tab_part[i].delta_z - 0.0003;
      dev.Transform.World :=
                    dev.Transform.World.Translation(t_tab_part[i].x,t_tab_part[i].z,t_tab_part[i].y);
      if t_tab_part[i].l then
        dev.SetTexture(0, particle_texture)
      else
        dev.SetTexture(0,part);
      particle_mesh.DrawSubset(0);
      dev.Material.EmissiveColor:=(ColorValue.Create(random(100),0,0)) ;




      particle_mesh.DrawSubset(0);

    t_tab_part[i].x:=t_tab_part[i].x + t_tab_part[i].delta_x -0.000001+(random(15)-7.5)/300;
    t_tab_part[i].y:=t_tab_part[i].y + t_tab_part[i].delta_y -0.000001+(random(15)-7.5)/300;
    t_tab_part[i].z:=t_tab_part[i].z+ t_tab_part[i].delta_z  -0.002;



//      begin
//          t_tab_part[i].delta_x:=(random(600)-300)/10000;
//          t_tab_part[i].delta_y:=(random(600)-300)/10000;
//          t_tab_part[i].delta_z:=-random(50)/k;
//          i:=i+1;
//      end
//    else
//      begin
        if (t_tab_part[i].z<0.1)or (t_tab_part[i].z>2.5) then
          begin
            t_tab_part[i]:=t_tab_part[len-1];
           len:=len-1;
           SetLength(t_tab_part,len);
          end
        else
          begin
            if (t_tab_part[i].l)and((trunc(t_tab_part[i].x-t_tab_part[i].to_x)=0)and(trunc(t_tab_part[i].y-t_tab_part[i].to_y)=0)) then
              begin
                t_tab_part[i].delta_x:=(random(600)-300)/100000;
                t_tab_part[i].delta_y:=(random(600)-300)/100000;

                if t_tab_part[i].z>2 then
                  begin
                    t_tab_part[i].l:=false;
                   // t_tab_part[i].delta_z := -t_tab_part[i].delta_z ;
                  end
                else
                  t_tab_part[i].delta_z:=random(10)/10;
//                t_tab_part[i].l:=false;

               end;
            if not(t_tab_part[i].l) then
                t_tab_part[i].delta_z := -t_tab_part[i].delta_z - 0.03;
                i:=i+1;

          end;
      end;
    end;
//  end;

destructor TParticle.Destroy;
begin
end;

end.
